import requests
import json
import pymysql
def query(id):

    #连接mysql
    db= pymysql.connect(host="rm-wz9n817r7d5ksg2ctgo.mysql.rds.aliyuncs.com",user="asplayer",
        password="Asplayer2016",db="task_master_mmobi",port=3306)
    #创建游标
    cur=db.cursor()
    sql="""SELECT * FROM ad_click WHERE ad_click.id="""+str(id)
    try:
        print(sql)
        cur.execute(sql)
        results = cur.fetchall()
    except Exception as e:
        raise e
    finally:
        db.close()
    return results
# data=dict()
# data1=[dict(
# idfa="77A73CD5-1EB4-2AF9-FD5F-99BA5B52A5BB",
# ip="112.22.195.116",
# ua="Mozilla/5.0 (iPad; CPU OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Mobile/14F89",
# url="http://iosapps.itunes.apple.com/apple-assets-us-std-000001/Purple115/v4/c6/0a/bb/c60abbbf-9393-00ec-0eb4-3811339ef963/pre-thinned3148606179666959446.thinned.signed.dpkg.ipa?accessKey=1531669847_8578225660183103978_iUdy5%2BzHsQmVq5QmPP2DIdeh%2FDd95ULRsQJ2h48PnpgLDqPrnLA4Ocsk2A0qVs0HBh6%2F4wd8tkHNbbVIPoPH2o%2FmfXYiAkPT%2BGzjaw0%2BOejPPuYjLUTiT1NHrTuTlGIvIB4dyvrbVymCeFjzvF7vvqLGetizRAxXtjG2gdPVt8DW9O5jU2W5vdJx5kPVaCxAJSAloweVVncUg%2B%2BF1sizxu%2B1s%2F0LatPeVEdJH5VR4IA%3D"
# )]
# data=dict(
#         data=data1,
#         time="1531475451"
#     )
#
# url = 'http://119.23.37.103:8802/json?channel=5'
# body =data
# headers = {'content-type': "application/json"}
# res=json.dumps(data).encode("utf-8").decode('unicode_escape')
# response = requests.post(url, data = json.dumps(body), headers = headers)
# print(res)

id=70245050
# while True:


while True:
    data = query(id)
    print(data)
    if data!=None:
        print(data[0][1])
        requests.adapters.DEFAULT_RETRIES = 5


        print(data[0][4])
        if data[0][4]==None:
            data1 = [dict(
                idfa="",
                ip=data[0][5],
                ua=data[0][6],
                url="http://iosapps.itunes.apple.com/apple-assets-us-std-000001/Purple115/v4/c6/0a/bb/c60abbbf-9393-00ec-0eb4-3811339ef963/pre-thinned"+k[3]+".thinned.signed.dpkg.ipa?accessKey=1531669847_8578225660183103978_iUdy5%2BzHsQmVq5QmPP2DIdeh%2FDd95ULRsQJ2h48PnpgLDqPrnLA4Ocsk2A0qVs0HBh6%2F4wd8tkHNbbVIPoPH2o%2FmfXYiAkPT%2BGzjaw0%2BOejPPuYjLUTiT1NHrTuTlGIvIB4dyvrbVymCeFjzvF7vvqLGetizRAxXtjG2gdPVt8DW9O5jU2W5vdJx5kPVaCxAJSAloweVVncUg%2B%2BF1sizxu%2B1s%2F0LatPeVEdJH5VR4IA%3D"
            )]
        elif data[0][5]==None:
            data1 = [dict(
                idfa=str(data[0][4],encoding="utf-8"),
                ip="",
                ua=data[0][6],
                url="http://iosapps.itunes.apple.com/apple-assets-us-std-000001/Purple115/v4/c6/0a/bb/c60abbbf-9393-00ec-0eb4-3811339ef963/pre-thinned" +
                    data[0][3] + ".thinned.signed.dpkg.ipa?accessKey=1531669847_8578225660183103978_iUdy5%2BzHsQmVq5QmPP2DIdeh%2FDd95ULRsQJ2h48PnpgLDqPrnLA4Ocsk2A0qVs0HBh6%2F4wd8tkHNbbVIPoPH2o%2FmfXYiAkPT%2BGzjaw0%2BOejPPuYjLUTiT1NHrTuTlGIvIB4dyvrbVymCeFjzvF7vvqLGetizRAxXtjG2gdPVt8DW9O5jU2W5vdJx5kPVaCxAJSAloweVVncUg%2B%2BF1sizxu%2B1s%2F0LatPeVEdJH5VR4IA%3D"
            )]
        elif data[0][6]==None:
            data1 = [dict(
                idfa=str(data[0][4],encoding="utf-8"),
                ip=data[0][5],
                ua="",
                url="http://iosapps.itunes.apple.com/apple-assets-us-std-000001/Purple115/v4/c6/0a/bb/c60abbbf-9393-00ec-0eb4-3811339ef963/pre-thinned" +
                    data[0][3] + ".thinned.signed.dpkg.ipa?accessKey=1531669847_8578225660183103978_iUdy5%2BzHsQmVq5QmPP2DIdeh%2FDd95ULRsQJ2h48PnpgLDqPrnLA4Ocsk2A0qVs0HBh6%2F4wd8tkHNbbVIPoPH2o%2FmfXYiAkPT%2BGzjaw0%2BOejPPuYjLUTiT1NHrTuTlGIvIB4dyvrbVymCeFjzvF7vvqLGetizRAxXtjG2gdPVt8DW9O5jU2W5vdJx5kPVaCxAJSAloweVVncUg%2B%2BF1sizxu%2B1s%2F0LatPeVEdJH5VR4IA%3D"
            )]
        else:
            data1 = [dict(
                idfa=data[0][4],
                ip=data[0][5],
                ua=data[0][6],
                url="http://iosapps.itunes.apple.com/apple-assets-us-std-000001/Purple115/v4/c6/0a/bb/c60abbbf-9393-00ec-0eb4-3811339ef963/pre-thinned" +
                    data[0][3] + ".thinned.signed.dpkg.ipa?accessKey=1531669847_8578225660183103978_iUdy5%2BzHsQmVq5QmPP2DIdeh%2FDd95ULRsQJ2h48PnpgLDqPrnLA4Ocsk2A0qVs0HBh6%2F4wd8tkHNbbVIPoPH2o%2FmfXYiAkPT%2BGzjaw0%2BOejPPuYjLUTiT1NHrTuTlGIvIB4dyvrbVymCeFjzvF7vvqLGetizRAxXtjG2gdPVt8DW9O5jU2W5vdJx5kPVaCxAJSAloweVVncUg%2B%2BF1sizxu%2B1s%2F0LatPeVEdJH5VR4IA%3D"
            )]
        print(44444)
        data = dict(
                data=data1,
                time=data[0][1]
            )
        #外网地址
        # url='http://119.23.37.103:8802/json?channel=5'
        #内网地址
        url = 'http://172.18.22.50:8802/json?channel=5'
        body =data
        headers = {'content-type': "application/json"}
        try:
            res=json.dumps(data).encode("utf-8").decode('unicode_escape')
            print(5555555)
            print(res)
            response = requests.post(url, data =res, headers = headers)
        except Exception:
            s = requests.session()
            s.keep_alive = False
        print(res)
        id=id+1
    else:
        print("数据为空")
